﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using ProgNetComponentsDx.Controls.Editors;

namespace ProgNetComponentsDx.Controls.Design.Editors
{
    public class TimeDurationEditor : UITypeEditor
    {
        protected IWindowsFormsEditorService editorService;
        TimeDurationEdit edit;

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            editorService = provider.GetService<IWindowsFormsEditorService>();
            if (editorService != null)
            {
                if (edit == null)
                {
                    edit = new TimeDurationEdit();
                    edit.KeyDown += new System.Windows.Forms.KeyEventHandler(edit_KeyDown);
                    edit.Properties.TimeMask = TimeMask.DayHourMinuteSecond;
                    edit.Properties.DisplayTimeChars = true;
                }
                
                if (context.Instance is RepositoryItemTimeDurationEdit)
                {
                    edit.Properties.Assign(context.Instance as RepositoryItemTimeDurationEdit);
                }
                else if (context.Instance is TimeDurationEdit)
                {
                    edit.Properties.Assign((context.Instance as TimeDurationEdit).Properties);
                }
                edit.Properties.MaxValue = RepositoryItemTimeDurationEdit.MAX_VAL;
                edit.EditValue = value;
                editorService.DropDownControl(edit);
                return edit.EditValue;
            }
            return base.EditValue(context, provider, value);
        }

        void edit_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                if (editorService != null)
                    editorService.CloseDropDown();
            }
        }
    }
}
